package com.littlebee.storysuper.activity;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.littlebee.storysuper.R;
import com.littlebee.storysuper.utils.Constant;
import com.littlebee.storysuper.utils.ToastUtil;

import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.event.ContactNotifyEvent;
import cn.jpush.im.api.BasicCallback;

import static cn.jpush.im.android.api.event.ContactNotifyEvent.Type.invite_accepted;
import static cn.jpush.im.android.api.event.ContactNotifyEvent.Type.invite_declined;
import static cn.jpush.im.android.api.event.ContactNotifyEvent.Type.invite_received;

public class ContactNotifyEventReceiver extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //这里只是为了展示注册事件接受者接口的用法，实际上开发者可以在任意类中注册事件接收者
        //，而不仅仅在Activity中。 下同
        JMessageClient.registerEventReceiver(this);
    }

    @Override
    protected void onDestroy() {
        JMessageClient.unRegisterEventReceiver(this);
        super.onDestroy();
    }

    public void onEvent(ContactNotifyEvent event) {
        String reason = event.getReason();
        String fromUsername = event.getFromUsername();
        String appkey = event.getfromUserAppKey();

        switch (event.getType()) {
            case invite_received://收到好友邀请
                ContactManager.acceptInvitation(event.getFromUsername(), Constant.IM_CHILD_KEY, new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        if (0 == responseCode) {
                            //接收好友请求成功
                            ToastUtil.displayShortToast("接收好友请求成功");
                        } else {
                            //接收好友请求失败
                        }
                    }
                });
                break;
            case invite_accepted://对方接收了你的好友邀请
                //...
                break;
            case invite_declined://对方拒绝了你的好友邀请
                //...
                break;
            case contact_deleted://对方将你从好友中删除
                //...
                break;
            default:
                break;
        }
    }

}

